﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyDaiLy.Context;
using QuanLyDaiLy.Model;
using QuanLyDaiLy.Platform;

namespace QuanLyDaiLy.View
{
    /// <summary>
    /// Interaction logic for WPhieuXuatHang.xaml
    /// </summary>
    public partial class WPhieuXuatHang : Page
    {

        #region VARIABLE
        private CPhieuXuatHangContext phieuxuat;
        private CChiTietPhieuXuatContext chitietxuat;
        private CMatHangContext mathang;
        private CDaiLyContext daily;
        private CDonViContext donvi;
        private List<CChiTietPhieuXuat> listChiTiet;

        private int mamathang, madonvi, soluong, dongia, thanhtien, tongtien, maphieuxuat;
        #endregion

        public WPhieuXuatHang()
        {
            InitializeComponent();
            TaoBang();
            listChiTiet = new List<CChiTietPhieuXuat>();
        }

        #region EVENT

        private void btnPhieuMoi_Click(object sender, RoutedEventArgs e)
        {
            LamMoi();
        }

        private void btnLapPhieu_Click(object sender, RoutedEventArgs e)
        {
            LapPhieu();
        }

        private void txtMaDaiLy_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                dtpNgayLap.Focus();
            }
        }

        private void txtMaDaiLy_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                var temp = daily.LayThongTin(int.Parse(txtMaDaiLy.Text));
                txtTenDaiLy.Text = temp.TenDaiLy;
                txtDiaChi.Text = temp.DiaChi;
                txtSoDienThoai.Text = temp.DienThoai;
            }
            catch 
            {
                txtTenDaiLy.Text = "";
                txtDiaChi.Text = "";
                txtSoDienThoai.Text = "";
            }
        }

        private void txtMaDaiLy_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (!Char.IsDigit((char)KeyInterop.VirtualKeyFromKey(e.Key)) &&
              !Char.IsControl((char)KeyInterop.VirtualKeyFromKey(e.Key)))
            {
                e.Handled = true;
            }
        }

        private void cbMatHang_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mamathang = ((sender as ComboBox).SelectedValue as CMatHang).MaMatHang;
            SuaChiTiet(mamathang, 0, 0, viewPhieuXuat.SelectedIndex);
        }

        private void cbDonVi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            madonvi = ((sender as ComboBox).SelectedValue as CDonVi).MaDonVi;
            SuaChiTiet(0, madonvi, 0, viewPhieuXuat.SelectedIndex);
        }

        private void txtSoLuong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    soluong = int.Parse((sender as TextBox).Text);
                    SuaChiTiet(0, 0, soluong, viewPhieuXuat.SelectedIndex);
                }
                catch
                { }

                if (ChoPhepThemChiTiet())
                {
                    ThemChiTiet();
                    LuuChiTiet();
                }
                else if (viewPhieuXuat.SelectedIndex == viewPhieuXuat.Items.Count - 1)
                {
                    MessageBox.Show("Không Được Để Trống Ô Nhập Liệu", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }

        private void cbMatHang_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (viewPhieuXuat.SelectedIndex == viewPhieuXuat.Items.Count - 1 &&
                viewPhieuXuat.SelectedIndex != -1 && (sender as ComboBox).SelectedIndex != -1 && KhongChoPhepSua())
            {
                e.Handled = true;
            }
        }

        private void cbDonVi_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (viewPhieuXuat.SelectedIndex == viewPhieuXuat.Items.Count - 1 &&
                viewPhieuXuat.SelectedIndex != -1 && (sender as ComboBox).SelectedIndex != -1 && KhongChoPhepSua())
            {
                e.Handled = true;
            }
        }

        private void txtSoLuong_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (viewPhieuXuat.SelectedIndex == viewPhieuXuat.Items.Count - 1 &&
                viewPhieuXuat.SelectedIndex != -1 && (sender as TextBox).Text != "" && KhongChoPhepSua())
            {
                e.Handled = true;
            }
        }

        #endregion

        #region FUNCTION

        private void TaoBang()
        {
            mathang = new CMatHangContext(CConstant.CONNECTION);
            phieuxuat = new CPhieuXuatHangContext(CConstant.CONNECTION);
            daily = new CDaiLyContext(CConstant.CONNECTION);
            chitietxuat = new CChiTietPhieuXuatContext(CConstant.CONNECTION);
            donvi = new CDonViContext(CConstant.CONNECTION);
        }

        private void LapPhieu()
        {
            try
            {
                if (ChoPhepLapPhieu())
                {
                    phieuxuat.Them(new CPhieuXuatHang(
                        int.Parse(txtMaDaiLy.Text),
                        dtpNgayLap.SelectedDate.Value,
                        tongtien));

                    for (int i = 0; i < listChiTiet.Count; i++)
                    {
                        chitietxuat.Them(listChiTiet[i]);
                    }
                    listChiTiet.Clear();
                    MessageBox.Show("Lập Phiếu Thành Công", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    txtMaDaiLy.Focus();
                    MessageBox.Show("Không Được Để Trống Các Ô Nhập Liệu", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch
            {
                MessageBox.Show("Lập Phiếu Thất Bại", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void LamMoi()
        {
            txtMaDaiLy.Clear();
            txtTenDaiLy.Clear();
            txtDiaChi.Clear();
            txtSoDienThoai.Clear();
            txtMaDaiLy.Focus();
            txtMaDaiLy.IsEnabled = true;
            dtpNgayLap.IsEnabled = true;
            txtMaDaiLy.Focus();

            maphieuxuat = phieuxuat.LayMaSoMoi();
            txtMaPhieuXuat.Text = maphieuxuat.ToString();
            dtpNgayLap.Text = DateTime.Now.ToShortDateString();
            viewPhieuXuat.Items.Clear();
            ThemChiTiet();
            tongtien = 0;
            
        }

        private void ThemChiTiet()
        {
            viewPhieuXuat.Items.Add(new
            {
                STT = (viewPhieuXuat.Items.Count + 1).ToString(),
                MatHang = mathang.LayDanhSachTatCa(),
                DonVi = donvi.LayDanhSachTatCa(),
                SoLuong = ""
            });
            mamathang = 0;
            madonvi = 0;
            soluong = 0;
        }

        private void LuuChiTiet()
        {
            thanhtien = dongia*soluong;
            tongtien += thanhtien;
            listChiTiet.Add(new CChiTietPhieuXuat(
                maphieuxuat,
                mamathang,
                madonvi,
                soluong,
                dongia,
                thanhtien));
        }

        private void SuaChiTiet(int mathang, int donvi, int soluong, int indexView)
        {
            if (indexView < listChiTiet.Count - 1)
            {
                if (mathang != 0)
                {
                    listChiTiet[indexView].MaMatHang = mathang;
                }

                if (donvi != 0)
                {
                    listChiTiet[indexView].MaDonViTinh = donvi;
                }

                if (soluong != 0)
                {
                    listChiTiet[indexView].SoLuong= soluong;
                }
            }
        }

        private bool ChoPhepThemChiTiet()
        {
            if (mamathang != 0 && madonvi != 0 && soluong != 0)
            {
                return true;
            }
            return false;
        }

        private bool KhongChoPhepSua()
        {
            if (mamathang == 0 && madonvi == 0 && soluong == 0)
            {
                return false;
            }
            return true;
        }

        private bool ChoPhepLapPhieu()
        {
            if (txtMaDaiLy.Text == "")
            {
                return false;
            }

            return true;
        }
        #endregion

      

       
    }
}
